package addon.w3.mdx;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;

import natv.CoreNative;

import jmetest.audio.TestJmexAudio;
import jmetest.renderer.loader.TestMd3JmeWrite;


import chaos.util.AbsPathResourceManager;
import chaos.util.MPQResourceManager;
import chaos.util.SmartResourceManager;
import chaos.util.ZIPResourceManager;

import com.jme.app.AbstractGame;
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.KeyBindingManager.KeyCodes;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jme.util.export.binary.BinaryImporter;
//import com.jmex.model.XMLparser.Converters.Md3ToJme;
import com.jmex.audio.AudioSystem;
import com.jmex.audio.AudioTrack;
import com.jmex.audio.AudioTrack.TrackType;
import com.jmex.audio.MusicTrackQueue.RepeatType;

public class TestMdxToJmeWrite extends SimpleGame {
	SmartResourceManager sMan = null;
	MDXMasterController controller = null;
	AudioSystem audio = null;
	
    public static void main(String[] args) {
    	TestMdxToJmeWrite app = new TestMdxToJmeWrite();
        app.start();
        
    }
    
    protected void quit() {
    	if(sMan != null) sMan.close();
    	super.quit();
    	
    }
    
    public void simpleUpdate() {
    	// update our audio system here:
        audio.update();
    	
    	for(int i=0;i<10;i++) {
    		if(KeyBindingManager.getKeyBindingManager().isValidCommand( String.valueOf(i) )) {
        		controller.setSequence(i);
        	}
    	}
    }
    
    protected void simpleInitGame() {
        try {
        	KeyBindingManager.getKeyBindingManager().set("1", KeyInput.KEY_1);
        	KeyBindingManager.getKeyBindingManager().set("2", KeyInput.KEY_2);
        	KeyBindingManager.getKeyBindingManager().set("3", KeyInput.KEY_3);
        	KeyBindingManager.getKeyBindingManager().set("4", KeyInput.KEY_4);
        	KeyBindingManager.getKeyBindingManager().set("5", KeyInput.KEY_5);
        	KeyBindingManager.getKeyBindingManager().set("6", KeyInput.KEY_6);
        	KeyBindingManager.getKeyBindingManager().set("7", KeyInput.KEY_7);
        	KeyBindingManager.getKeyBindingManager().set("8", KeyInput.KEY_8);
        	KeyBindingManager.getKeyBindingManager().set("9", KeyInput.KEY_9);
        	KeyBindingManager.getKeyBindingManager().set("0", KeyInput.KEY_0);
        	
        	MPQResourceManager.singleton().init();
        	ZIPResourceManager zipMan = null;
        	//c = new ZIPResourceManager("D:/moddn/w3x/www_mdx/3/Grom_Hellscream.zip");
        	//zipMan.init();
        	//AbsPathResourceManager pathMan = new AbsPathResourceManager("D:/www_mdx/3/Grom_Hellscream");
        	AbsPathResourceManager pathMan = new AbsPathResourceManager("D:/moddn/w3x/war3/mdl");
        	pathMan.init();
        	sMan = new SmartResourceManager(MPQResourceManager.singleton(), zipMan, pathMan);
        	
        	display.getRenderer().setBackgroundColor( ColorRGBA.darkGray );
        	MDXToJme converter = new MDXToJme(sMan);
        	//String path = "Doodads\\Terrain\\Cliffs\\CliffsCCAB0.mdx";
        	String path = "Buildings\\Other\\DarkPortal\\DarkPortal.mdx";
        	//String path = "Units\\Undead\\EvilSylvanas\\EvilSylvanas.mdx";
        	//String path = "Units\\NightElf\\Huntress\\Huntress.mdx";
        	//String path = "Units\\Creeps\\ForestTroll\\ForestTroll.mdx";
        	//String path = "DuneWorm.mdx";
    		//String path = "Units\\Creeps\\OrcWarlock\\OrcWarlock.mdx";//"Abilities\\Spells\\Demon\\DarkPortal\\DarkPortalTarget.mdx";//
        	//String path = "Doodads\\Terrain\\LordaeronTree\\LordaeronTree9.mdx";
        	//String path = "Grom Hellscream.mdx";
        	//String path = "Abilities\\Weapons\\HarpyMissile\\HarpyMissile.mdx";
        	
        	lightState.setEnabled(true);
            long time=System.currentTimeMillis();
            converter.load(path);
            Spatial r = converter.constructMesh();
            //r.setModelBound(new BoundingBox());
            //r.updateModelBound();
            r.getLocalRotation().fromAngles(0.0f,-1.0f,-1.5f);
            System.out.println("Finished loading time is "+(System.currentTimeMillis()-time));
            controller = (MDXMasterController)converter.getController();
            controller.setSequence(MDXMasterController.STAND);
            //controller.setSequence("Stand Work");
            controller.setRepeat(-1);
            r.addController( controller );
            rootNode.attachChild(r);
//            cam.setFrustumPerspective(45.0f, (float) display.getWidth()
//                    / (float) display.getHeight(), 1f, 10000f);//farPlane);//
            cam.setLocation(new Vector3f(.0f, 0.0f, 250));
            
            
            // grab a handle to our audio system.
            audio = AudioSystem.getSystem();
            //set background sound
            // setup a music score for our demo
            AudioTrack sound = AudioSystem.getSystem().createAudioTrack(new java.io.File("F:\\Music\\gm\\jericho\\04 - Path of the Righteous.ogg").toURL(), true);
            sound.setType(TrackType.MUSIC);
            sound.setRelative(true);
            sound.setTargetVolume(0.7f);
            sound.setLooping(false);
            audio.getMusicQueue().setRepeatType(RepeatType.ALL);
            audio.getMusicQueue().setCrossfadeinTime(2.5f);
            audio.getMusicQueue().setCrossfadeoutTime(2.5f);
            audio.getMusicQueue().addTrack(sound);
            audio.getMusicQueue().play();
        } catch (Exception e) {
        	finished = true;
            e.printStackTrace();
        }
    }
}